package com.leyou.search.controller;

import com.leyou.search.dto.SearchParamDTO;
import com.leyou.search.entity.Goods;
import com.leyou.search.service.SearchService;
import com.leyou.starter.elastic.dto.PageInfo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * @Author: SGS
 * @Date : 2020/11/22 23:14
 * @Version :
 */
@RestController
@RequestMapping("goods")
public class SearchController {
    private final SearchService searchService;

    public SearchController(SearchService searchService) {
        this.searchService = searchService;
    }

    @GetMapping("/initialization")
    public ResponseEntity<String> init(){
        searchService.createIndexAndMapping();
        searchService.loadData();
        return ResponseEntity.ok("导入成功");
    }

    @GetMapping("/suggestion")
    public Mono<List<String>> getSuggestion(@RequestParam String key){
       return searchService. getSuggestion(key);
    }

    @PostMapping("/list")
    public Mono<PageInfo<Goods>> searchGoods(@RequestBody SearchParamDTO request){
        return searchService.searchGoods(request);
    }

}
